package src.com.neusoft.nep.controller;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.stage.Stage;
import src.com.neusoft.nep.common.FileUtils;
import src.com.neusoft.nep.entity.DetailedAddress;
import src.com.neusoft.nep.entity.FeedbackData;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class PubliclySupervisedDateDetailsViewController {
    @FXML
    private Button btn_back;
    @FXML
    private TableView<FeedbackData> tableView1;
    @FXML
    private TableView<DetailedAddress> tableView2;
    @FXML
    private TableColumn<FeedbackData, String> colFeedbackerName, colEstPollutionLevel, colFeedbackDate, colPhoneNumber,colProvince,colCity,colDetails;
    @FXML
    private TableColumn<DetailedAddress,String>colDetailedAddress;
    private ObservableList<FeedbackData> tableList1 = FXCollections.observableArrayList();
    private ObservableList<DetailedAddress> tableList2 = FXCollections.observableArrayList();

    private Map firstMap = new HashMap();
    private Map secondMap = new HashMap();

    @FXML
    public void initialize() {

        tableView1.setItems(tableList1);

        colFeedbackerName.setCellValueFactory(new PropertyValueFactory<>("feedbackerName"));
        colEstPollutionLevel.setCellValueFactory(new PropertyValueFactory<>("estPollutionLevel"));
        colFeedbackDate.setCellValueFactory(new PropertyValueFactory<>("feedbackDate"));
        colPhoneNumber.setCellValueFactory(new PropertyValueFactory<>("phoneNumber"));
        colProvince.setCellValueFactory(new PropertyValueFactory<>("locProvince"));
        colCity.setCellValueFactory(new PropertyValueFactory<>("locCity"));
        colDetails.setCellValueFactory(new PropertyValueFactory<>("details"));

        firstMap = FileUtils.readMapObject("FeedbackData.txt");
        tableList1.addAll(firstMap.values());
        colFeedbackerName.setCellFactory(TextFieldTableCell.forTableColumn());
        colFeedbackerName.setOnEditCommit(event -> {
            FeedbackData c = event.getRowValue();
            String newLocCity = event.getNewValue();
            c.setLocCity(newLocCity);
            firstMap.put(c.getLocCity(), c);
            FileUtils.writeMapObject("FeedbackData.txt", firstMap);
        });

        tableView2.setItems(tableList2);
        colDetailedAddress.setCellValueFactory(new PropertyValueFactory<>("detailedAddress"));
        secondMap = FileUtils.readMapObject("DetailedAddress.txt");
        tableList2.addAll(secondMap.values());
        colDetailedAddress.setCellFactory(TextFieldTableCell.forTableColumn());
        colDetailedAddress.setOnEditCommit(event -> {
            DetailedAddress d = event.getRowValue();
            String newLocCity = event.getNewValue();
            d.setDetailedAddress(newLocCity);
            secondMap.put(d.getDetailedAddress(), d);
            FileUtils.writeMapObject("DetailedAddress.txt", firstMap);
        });
    }
    public void back() throws IOException {
        // 获取当前舞台
        Stage currentStage = (Stage) btn_back.getScene().getWindow();
        // 关闭当前舞台
        currentStage.close();
    }
}
